﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using T_BPI.Classes.Model;
using T_BPI.Classes.Base;

namespace T_BPI
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private ExcelPort excelPort;
        private GMDH gmdh;

        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {/*
            GMDH gmdh = new GMDH(2, 8, 8);
            double[,] inputData = new double[8,3];
            double[] result = new double[8];
            int i = 0; int j = 0; 
            inputData[i,j++ % 3] = 0; inputData[i,j++ % 3] = 0; inputData[i,j++ % 3] = 0; result[i] = 0;
            i++;
            inputData[i,j++ % 3] = 0; inputData[i,j++ % 3] = 0; inputData[i,j++ % 3] = 1; result[i] = 1;
            i++;
            inputData[i,j++ % 3] = 0; inputData[i,j++ % 3] = 1; inputData[i,j++ % 3] = 0; result[i] = 0;
            i++;
            inputData[i,j++ % 3] = 0; inputData[i,j++ % 3] = 1; inputData[i,j++ % 3] = 1; result[i] = 1;
            i++;
            inputData[i,j++ % 3] = 1; inputData[i,j++ % 3] = 0; inputData[i,j++ % 3] = 0; result[i] = 0;
            i++;
            inputData[i,j++ % 3] = 1; inputData[i,j++ % 3] = 0; inputData[i,j++ % 3] = 1; result[i] = 1;
            i++;
            inputData[i,j++ % 3] = 1; inputData[i,j++ % 3] = 1; inputData[i,j++ % 3] = 0; result[i] = 1;
            i++;
            inputData[i,j++ % 3] = 1; inputData[i,j++ % 3] = 1; inputData[i,j++ % 3] = 1; result[i] = 1;
            gmdh.training(inputData, result);

            double[] x = new double[3];
            x[0] = 0;
            x[1] = 0;
            x[2] = 0;
            Utils.Log("result - {0}",gmdh.testGMDH(x));
           */
            excelPort = new ExcelPort(this.progressBar);
            Action action = excelPort.ReadFile;
            Task readingTask = new Task(action);
            readingTask.Start();
            readingTask.ContinueWith(t =>
            {
                foreach (var item in excelPort.columnNames)
                    this.fieldList.Dispatcher.Invoke(() =>
                        {
                            this.fieldList.Items.Add(item);
                        });
            });
        }

        private void AddToInput(object sender, RoutedEventArgs e)
        {
            List<object> itemsToRemove = new List<object>();
            foreach (var selecteditem in this.fieldList.SelectedItems)
            {
                var strItem = selecteditem as String;
                this.selectedFieldList.Items.Add(selecteditem);
                itemsToRemove.Add(selecteditem);
                System.Diagnostics.Debug.WriteLine(strItem);
            }
            foreach (var selecteditem in itemsToRemove)
            {
                this.fieldList.Items.Remove(selecteditem);
            }
        }

        private void RemoveFromInput(object sender, RoutedEventArgs e)
        {
            List<object> itemsToRemove = new List<object>();
            foreach (var selecteditem in this.selectedFieldList.SelectedItems)
            {
                var strItem = selecteditem as String;
                this.fieldList.Items.Add(selecteditem);
                itemsToRemove.Add(selecteditem);
            }
            foreach (var selecteditem in itemsToRemove)
            {
                this.selectedFieldList.Items.Remove(selecteditem);
            }
        }

        private void AnylizeData(object sender, RoutedEventArgs e)
        {
            int inputCount = this.selectedFieldList.Items.Count;
            if (inputCount <= 2 || excelPort == null)
            {
                MessageBox.Show("Надо выбрать больше 2х параметров");
                return;
            }

            if (this.Output.Content == null)
            {
                MessageBox.Show("Надо выбрать параметр на выход");
                return;
            }

            double[,] inputData = new double[excelPort.row, inputCount];
            double[] result = new double[excelPort.row];

            List<int> indexsToCopy = new List<int>();
            int outputIndex = 0;
            foreach (var selecteditem in this.selectedFieldList.Items)
            {
                for (int i = 0; i < excelPort.col; i++)
                {
                    if (excelPort.columnNames[i].Equals(selecteditem))
                    {
                        indexsToCopy.Add(i);
                        Utils.Log("{0}", selecteditem);
                    }
                    if (this.Output.Content.Equals(excelPort.columnNames[i]))
                    {
                        outputIndex = i;
                    }
                }
            }

            for (int i = 0; i < inputCount; i++) {
                for (int j = 0; j < excelPort.row; j++)
                    inputData[j,i] = excelPort.inputData[j, indexsToCopy.ElementAt(i)];
            }

            for (int j = 0; j < excelPort.row; j++)
                result[j] = excelPort.inputData[j, outputIndex];


            gmdh = new GMDH(inputCount, excelPort.row, excelPort.row * 2 / 3);
            gmdh.training(inputData, result);

            double[] testData = new double[5];
            testData[0] = -23.4;
            testData[1] = -14.9;
            testData[2] = -10.1;
            testData[3] = 885.0;
            testData[4] = 50;
            Utils.Log("SOme data - {0}", gmdh.testGMDH(testData));
        }

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            this.Output.Content = this.fieldList.SelectedItem;
        }

    }
}
